<template>
  <div class="login">
    <!-- 头部导航S -->
    <van-nav-bar title="登录" class="page-nav-bar" >
      <template #left>
        <van-icon @click="$router.back()"  color='#fff' name="cross" />
  </template>
    </van-nav-bar>
    <!-- 头部导航E -->

    <!-- 表单登录S -->
    <van-form @submit="onSubmit" ref="form">
      <!-- 手机号 -->
      <van-field
        v-model="form.mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="[
          { required: true, message: '请输入手机号' },
          {
            pattern:
              /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '手机号码格式不正确',
          },
        ]"
      >
        <template #left-icon>
          <i class="toutiao toutiao-shouji"></i>
        </template>
      </van-field>

      <!-- 验证码 -->
      <van-field
        v-model="form.code"
        name="code"
        placeholder="请输入验证码"
        :rules="[
          { required: true, message: '请输入验证码' },
          { pattern: /^[0-9]{6}$/, message: '验证码格式不正确' },
        ]"
      >
        <template #left-icon>
          <i class="toutiao toutiao-yanzhengma"></i>
        </template>

        <template #button>
          <van-count-down
          @finish='isShowCountDown = false'
            v-if="isShowCountDown"
            :time="60 * 1000"
            format="ss s"
          />
          <van-button
            v-else
            @click="onSend"
            native-type="button"
            class="send-btn"
            size="small"
            >
            发送验证码
            </van-button>

        </template>
      </van-field>

      <!-- 登录 -->
      <div style="margin: 16px">
        <van-button
          class="login-btn"
          round
          block
          type="info"
          native-type="submit"
        >
          登录
        </van-button>
      </div>
    </van-form>
    <!-- 表单登录E -->
  </div>
</template>

<script>
import { getCode, login } from '@/api/user.js'
import { Toast } from 'vant'
export default {
  data () {
    return {
      form: {
        mobile: '',
        code: ''
      },
      isShowCountDown: false
    }
  },

  created () {

  },

  methods: {
    async  onSubmit () {
      try {
        const res = await login(this.form)
        // 存的data 是{}
        this.$store.commit('initUser', res)
        // console.log(res)
        this.$router.push('/profile')
      } catch (err) {
        Toast.fail('登陆失败')
      }
    },
    async onSend () {
      try {
        // 传入name 来验证单个或部分表单项
        await this.$refs.form.validate('mobile')
        await getCode(this.form.mobile)
        Toast.success('发送成功')
        this.isShowCountDown = true
      } catch (err) {
        console.dir(err)
        // 如果错误里没有response 那就代表不是请求的错误
        // 不是请求错误就是 校验的问题 如果是校验问题 不用处理直接return出去
        if (!err.response) return
        if (err.response && err.response.status === 429) {
          return Toast.fail('发送频繁，稍后重试')
        }
        Toast.fail('网络异常')
      }
    }
  }
}
</script>

<style scoped lang="less">
.login {
  .login-btn {
    width: 694px;
    height: 88px;
    background: #6db4fb;
    border: none;
    border-radius: 10px;
  }
  .toutiao {
    font-size: 37px;
  }
  .send-btn {
    width: 152px;
    height: 46px;
    background: #ededed;
    border-radius: 23px;
    padding: 0;
    font-size: 22px;
    color: #666666;
  }
}
</style>
